﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2_8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            string s = txtInput.Text;
            if (s.Length != 1)
            {
                MessageBox.Show("只能输入1个字符！");
                txtInput.Text = "";
                txtInput.Focus();
                return;
            }

            char c = s[0];
            int loopCount, baseNum;

            if (Char.IsNumber(c))
            {
                if (c < '1' || c > '9')
                {
                    MessageBox.Show("数字必须是1-9！");
                    return;
                }
                baseNum = 49; // '1'的ASCII码
            }
            else if (Char.IsUpper(c))
                baseNum = 65; // 'A'的ASCII码
            else if (Char.IsLower(c))
                baseNum = 97; // 'a'的ASCII码
            else
            {
                MessageBox.Show("您输入的字符不合法，只能是大小写字母或数字1-9！");
                txtInput.Text = "";
                txtInput.Focus();
                return;
            }

            loopCount = c - baseNum + 1; // 计算循环次数（行数）
            lblOutput.Text = "";

            for (int i = 0; i < loopCount; i++)
            {
                // 打印空格
                for (int j = loopCount - 1; j > i; j--)
                    lblOutput.Text += "  ";

                // 打印字符
                for (int k = 0; k < 2 * i + 1; k++)
                    lblOutput.Text += (char)(baseNum + i);

                lblOutput.Text += Environment.NewLine;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtInput.Text = "";
            lblOutput.Text = "";
            txtInput.Focus();
        }
    }
}
